package com.example.elmmerchants.Order_Setting;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Book_Order_Setting extends AppCompatActivity {

    Switch is_open_book_order;
    TextView is_all_day, start_time, end_time, served_which_time_remind_text_view, start_time_2, end_time_2, served_open_time, served_end_time, start_day_text_view, end_day_text_view;
    RelativeLayout which_time_can_book, book_order_setting, how_many_time_finish_order;
    LinearLayout time_2;

    String email, open_time, close_time, open_day, open_time_2, close_time_2;
    int is_all_day_can_book_order, can_served_start_time, can_served_end_time, served_which_time_remind;
    boolean open_book_order;
    String[] start_day_list = new String[]{"0", "1", "2", "3", "4", "5", "6", "7"};
    String[] day_1 = new String[]{"1", "2", "3", "4", "5", "6", "7"};
    String[] day_2 = new String[]{"2", "3", "4", "5", "6", "7"};
    String[] day_3 = new String[]{"3", "4", "5", "6", "7"};
    String[] day_4 = new String[]{"4", "5", "6", "7"};
    String[] day_5 = new String[]{"5", "6", "7"};
    String[] day_6 = new String[]{"6", "7"};
    String[] day_7 = new String[]{"7"};
    String[] all_min = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_order_setting);

        email = getIntent().getStringExtra("email");
        is_all_day_can_book_order = getIntent().getIntExtra("is_all_day_can_book_order", 0);
        can_served_start_time = getIntent().getIntExtra("can_served_start_time", 0);
        can_served_end_time = getIntent().getIntExtra("can_served_end_time", 0);
        served_which_time_remind = getIntent().getIntExtra("served_which_time_remind", 0);
        open_book_order = getIntent().getBooleanExtra("open_book_order", false);
        open_time = getIntent().getStringExtra("open_time");
        close_time = getIntent().getStringExtra("close_time");
        open_day = getIntent().getStringExtra("open_day");
        open_time_2 = getIntent().getStringExtra("open_time_2");
        close_time_2 = getIntent().getStringExtra("close_time_2");

        is_open_book_order = findViewById(R.id.is_open_book_order);
        is_all_day = findViewById(R.id.is_all_day);
        start_time = findViewById(R.id.start_time);
        end_time = findViewById(R.id.end_time);
        served_which_time_remind_text_view = findViewById(R.id.served_which_time_remind);
        which_time_can_book = findViewById(R.id.which_time_can_book);
        book_order_setting = findViewById(R.id.book_order_setting);
        how_many_time_finish_order = findViewById(R.id.how_many_time_finish_order);

        start_time.setText(String.valueOf(can_served_start_time));
        end_time.setText(String.valueOf(can_served_end_time));
        served_which_time_remind_text_view.setText(String.valueOf(served_which_time_remind));

        if (is_all_day_can_book_order == 1) {
            is_all_day.setText("全天均支持");
        }
        else {
            is_all_day.setText("仅在营业时间支持");
        }

        if (open_book_order) {
            is_open_book_order.setChecked(true);
        }
        else {
            is_open_book_order.setChecked(false);
        }

        is_open_book_order.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_book_order.isChecked()) {
                    set_open_or_close_book_order(1, email);
                }
                else {
                    set_open_or_close_book_order(0, email);
                }
            }
        });

        which_time_can_book.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();
            }
        });

        book_order_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_which_time_can_give_order_dialog();
            }
        });

        how_many_time_finish_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_how_many_time_finish_order_dialog();
            }
        });
    }

    void set_how_many_time_finish_order_dialog() {
        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.choose_how_many_time_finish_order_bottomsheet);
        TextView time_text_view = dialog.findViewById(R.id.time_text_view);
        time_text_view.setText("0");
        NumberPicker how_many_time = dialog.findViewById(R.id.how_many_time);
        how_many_time.setMaxValue(all_min.length - 1);
        how_many_time.setDisplayedValues(all_min);
        how_many_time.setMinValue(0);
        how_many_time.setValue(0);
        how_many_time.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                time_text_view.setText(String.valueOf(numberPicker.getValue()));
            }
        });
        Button save = dialog.findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                served_which_time_remind = how_many_time.getValue();
                served_which_time_remind_text_view.setText(String.valueOf(served_which_time_remind));

                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "update `merchant_book_order_setting` set served_which_time_remind = ? where email = ?";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, how_many_time.getValue());
                        preparedStatement.setString(2, email);
                        preparedStatement.executeUpdate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    runOnUiThread(() -> {
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        dialog.dismiss();
                    });
                });
            }
        });
        Button close_page = dialog.findViewById(R.id.close_page);
        close_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setGravity(Gravity.BOTTOM);
    }

    void set_which_time_can_give_order_dialog() {
        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.choose_can_give_order_bottomsheet);
        start_day_text_view = dialog.findViewById(R.id.start_day_text_view);
        start_day_text_view.setText("第0天");
        end_day_text_view = dialog.findViewById(R.id.end_day_text_view);
        end_day_text_view.setText("第0天");
        NumberPicker start_day = dialog.findViewById(R.id.start_day);
        start_day.setDisplayedValues(start_day_list);
        start_day.setMaxValue(start_day_list.length - 1);
        start_day.setMinValue(0);
        start_day.setValue(0);
        NumberPicker end_day = dialog.findViewById(R.id.end_day);
        start_day.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                start_day_text_view.setText("第" + numberPicker.getValue() + "天");
                end_day_text_view.setText("第" + numberPicker.getValue() + "天");

                if (numberPicker.getValue() == 0) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(start_day_list.length - 1);
                    end_day.setDisplayedValues(start_day_list);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 1) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_1.length - 1);
                    end_day.setDisplayedValues(day_1);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 2) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_2.length - 1);
                    end_day.setDisplayedValues(day_2);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 3) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_3.length - 1);
                    end_day.setDisplayedValues(day_3);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 4) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_4.length - 1);
                    end_day.setDisplayedValues(day_4);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 5) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_5.length - 1);
                    end_day.setDisplayedValues(day_5);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 6) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_6.length - 1);
                    end_day.setDisplayedValues(day_6);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
                else if (numberPicker.getValue() == 7) {
                    end_day.setDisplayedValues(null);
                    end_day.setMaxValue(day_7.length - 1);
                    end_day.setDisplayedValues(day_7);
                    end_day.setMinValue(0);
                    end_day.setValue(0);
                }
            }
        });
        end_day.setMaxValue(start_day_list.length - 1);
        end_day.setDisplayedValues(start_day_list);
        end_day.setMinValue(0);
        end_day.setValue(0);
        end_day.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                int day = numberPicker.getValue() + start_day.getValue();
                end_day_text_view.setText("第" + day + "天");
            }
        });
        Button close_page = dialog.findViewById(R.id.close_page);
        close_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        Button save = dialog.findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                can_served_start_time = start_day.getValue();
                can_served_end_time = end_day.getValue();
                start_time.setText(String.valueOf(can_served_start_time));
                end_time.setText(String.valueOf(can_served_end_time));

                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "update `merchant_book_order_setting` set can_served_start_time = ?, can_served_end_time = ? where email = ?";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, start_day.getValue());
                        preparedStatement.setInt(2, end_day.getValue());
                        preparedStatement.setString(3, email);
                        preparedStatement.executeUpdate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    runOnUiThread(() -> {
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        dialog.dismiss();
                    });
                });
            }
        });

        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setGravity(Gravity.BOTTOM);
    }

    void set_dialog() {
        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.choose_which_order_time_bottomsheet_layout);
        RadioGroup choose_which_time_can_book_order = dialog.findViewById(R.id.choose_which_time_can_book_order);
        RadioButton just_in_open_shop_can_book_order = dialog.findViewById(R.id.just_in_open_shop_can_book_order);
        RadioButton can_all_day_book_order = dialog.findViewById(R.id.can_all_day_book_order);
        if (is_all_day_can_book_order == 1) {
            can_all_day_book_order.setChecked(true);
        }
        else {
            just_in_open_shop_can_book_order.setChecked(true);
        }
        choose_which_time_can_book_order.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton type = dialog.findViewById(radioGroup.getCheckedRadioButtonId());
                if (type.getText().toString().equals("仅营业时间内支持下预订单")) {
                    is_all_day_can_book_order = 0;
                }
                else {
                    is_all_day_can_book_order = 1;
                }
            }
        });
        RelativeLayout set_open_shop_time = dialog.findViewById(R.id.set_open_shop_time);
        set_open_shop_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        served_open_time = dialog.findViewById(R.id.start_time);
        served_open_time.setText(open_time);
        served_end_time = dialog.findViewById(R.id.end_time);
        served_end_time.setText(close_time);
        time_2 = dialog.findViewById(R.id.time_2);
        start_time_2 = dialog.findViewById(R.id.start_time_2);
        end_time_2 = dialog.findViewById(R.id.end_time_2);
        if (!open_time_2.isEmpty()) {
            time_2.setVisibility(View.VISIBLE);
            start_time_2.setText(open_time_2);
            end_time_2.setText(close_time_2);
        }
        LinearLayout change_the_open_time = dialog.findViewById(R.id.change_the_open_time);
        change_the_open_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(dialog.getContext(), Set_Open_Shop_Time.class);
                intent.putExtra("open_day", open_day);
                intent.putExtra("email", email);
                intent.putExtra("open_time", open_time);
                intent.putExtra("close_time", close_time);
                startActivityForResult(intent, 1);
            }
        });
        Button close_page = dialog.findViewById(R.id.close_page);
        close_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        Button save = dialog.findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "update `merchant_book_order_setting` set is_all_day_can_book_order = ? where email = ?";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, is_all_day_can_book_order);
                        preparedStatement.setString(2, email);
                        preparedStatement.executeUpdate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    runOnUiThread(() -> {
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        dialog.dismiss();
                    });
                });
            }
        });
        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setGravity(Gravity.BOTTOM);
    }

    void set_open_or_close_book_order(int i, String email) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            try {
                Connection_SQL userSQL = new Connection_SQL();
                Connection connection = userSQL.connection();
                String sql = "update `merchant_book_order_setting` set is_open_book_order = ? where email = ?";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, i);
                preparedStatement.setString(2, email);
                preparedStatement.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        runOnUiThread(() -> {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (i == 1) {
                is_open_book_order.setChecked(true);
                open_book_order = true;
            }
            else {
                is_open_book_order.setChecked(false);
                open_book_order = false;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1: {
                if (data != null) {
                    open_time = data.getStringExtra("open_time");
                    close_time = data.getStringExtra("end_time");
                    open_time_2 = data.getStringExtra("open_time_2");
                    close_time_2 = data.getStringExtra("end_time_2");
                    if (!open_time_2.isEmpty()) {
                        time_2.setVisibility(View.VISIBLE);
                        served_open_time.setText(open_time);
                        served_end_time.setText(close_time);
                        start_time_2.setText(open_time_2);
                        end_time_2.setText(close_time_2);
                    }
                    else {
                        time_2.setVisibility(View.GONE);
                        served_open_time.setText(open_time);
                        served_end_time.setText(close_time);
                    }
                }
                break;
            }
        }
    }
}